package com.smartStatus.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.ribbon.proxy.annotation.Http;
import com.smartStatus.admin.service.SysMenuService;
import com.smartStatus.admin.service.SysRoleService;
import com.smartStatus.api.entity.SysMenu;
import com.smartStatus.api.entity.SysMenuDetails;
import com.smartStatus.api.entity.SysRole;
import com.smartStatus.api.vo.SysMenuVo;
import com.smartStatus.core.api.RedisKey;
import com.smartStatus.core.api.VerficationCode;
import com.smartStatus.core.constant.StatusUser;
import com.smartStatus.core.util.R;
import com.smartStatus.security.annoation.Ignore;
import com.smartStatus.security.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName : SysMenuController
 * @Author : lby
 * @Date: 2020/12/8 11:25
 * @Description : 系统菜单管理
 */
@RestController
@RequestMapping("/menu")
@AllArgsConstructor
@Slf4j
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @Ignore(value = false)
    @GetMapping("/list/{flag}")
    public R getMenuList(@PathVariable("flag") Boolean flag) {
        return sysMenuService.getMenuTree(flag);
    }

    /**
     * 分页数据
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @GetMapping("/page")
    public R page(Page<SysMenu> page, SysMenu sysMenu) {
        return R.ok(sysMenuService.pageList(page, Wrappers.query(sysMenu), sysMenu));
    }

    /**
     * 子菜单数据展示
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @PostMapping("/sub-compontents")
    public R getSubMenus(@RequestBody SysMenu sysMenu) {
        List<SysMenuVo> sysMenuVos = sysMenuService.childrenList(sysMenu);
        return R.ok(sysMenuVos);
    }

    /**
     * 菜单添加
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @PostMapping("/save")
    public R saveMenus(@RequestBody SysMenuVo sysMenuVo) {
        return sysMenuService.saveMenuList(sysMenuVo);
    }

    /**
     * 菜单修改
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @PostMapping("/edit")
    public R editMenus(@RequestBody SysMenu sysMenu) {

        return R.ok(sysMenuService.editMenu(sysMenu));
    }

    /**
     * 菜单删除
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @PostMapping("/delete")
    public R delMenus(@RequestBody SysMenu sysMenu) {
        return R.ok(sysMenuService.menuDel(sysMenu));
    }

    /**
     * 获取当前菜单父菜单id
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @PostMapping("/get-parent-id")
    public R getParentId(@RequestBody Integer id) {
        return R.ok(sysMenuService.getParentId(id));
    }


    /**
     * 根据当前id查询父菜单信息
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_view')")
    @GetMapping("/get-parent-info/{menuId}")
    public R getParentInfo(@PathVariable("menuId") Integer menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        return R.ok(sysMenuService.getById(menu.getMenuParentId()));
    }


    /**
     * 删除缓存中的菜单
     * @return
     */
    @GetMapping("/del-cache-menu/{userId}")
    @Ignore(value = false)
    public R deleteMenuTree(@PathVariable("userId") Integer userId){
        return sysMenuService.deleteMenuTree(userId);
    }


}
